﻿// -----------------------------------------------------------------------
// <copyright file="BasicCalculator.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Cspl.Calculator.Lib
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Cspl.Calculator.Lib.Interface;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class BasicCalculator : IBasicCalculator
    {
        public int Add(int a, int b)
        {
            return a + b;
        }

        public int Mul(int a, int b)
        {
            return a * b;
        }

        public int Div(int a, int b)
        {
            int retval;

            try
            {
                retval = a / b;
            }
            catch (Exception ex)
            {
                Console.WriteLine("An Exception occurred : " + ex.Message);
                retval = 0;
            }

            return retval;
        }

        public int Sub(int a, int b)
        {
            return a - b;
        }
        
        //public int AddList(IEnumerable<int> listOfNumber)
        //{
        //    int retval = 0;

        //    foreach (int number in listOfNumber)
        //    {
        //        retval += number;
        //    }

        //    return retval;
        //}

        public int ComplexListOperation(IEnumerable<int> listOfNumber, CalOperations calop)
        {
            int retval = (calop == CalOperations.Mul || calop == CalOperations.Div) ? 1 : 0;

            foreach (int number in listOfNumber)
            {
                switch (calop)
                {
                    case CalOperations.Add:
                        retval = this.Add(retval, number);
                        break;
                    case CalOperations.Mul:
                        retval = this.Mul(retval, number);
                        break;
                    case CalOperations.Div:
                        retval = this.Div(retval, number);
                        break;
                    case CalOperations.Sub:
                        retval = this.Sub(retval, number);
                        break;
                    default:
                        break;
                }
            }

            return retval;
        }
    }
}
